package com.myk.game.heroscuffle.skills;

import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.game.Skill;

public class Skill_PoXiang extends Skill {

    public static final int HEALING_AMOUNT = 40;
    public static final int DROP_ATK = 40;

    private Hero intentTarget;

    public void setIntentTarget(Hero intentTarget) {
        this.intentTarget = intentTarget;
    }

    public Skill_PoXiang() {
        super("迫降", "指定一位英雄，临时降低其攻击力，并消除目标对自己的仇恨。之后与自己共同恢复一定生命值。", 3, 3);
    }

    @Override
    protected void onTrigger() {
        if (hostHero.state.isPlayer) {
            selectTargetHero(0, true);
        }
        else {
            if (intentTarget == null) {
                intentTarget = game.pool.getAnyoneAliveHero(hostHero);
            }
            skillEffect(intentTarget);
        }
    }

    //玩家选择技能施放目标
    private void selectTargetHero(final int index, final boolean isFirst) {
        game.executor.submit(() -> {
            Hero curSelectHero = game.pool.getAllAliveHeroes().get(index);
            if (curSelectHero == hostHero) {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }
            game.ui.setTip("请选择你要施放的目标,当前待选:" + curSelectHero.getObviousName());
            game.ui.resetCtrl();
            game.ui.addCtrl("选中", () -> game.executor.submit(() -> {
                skillEffect(curSelectHero);
                game.executor.resume(); //通知阻塞的线程继续执行以后的代码
            }));
            game.ui.addCtrl("切换", () -> game.executor.submit(() -> {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }));
        });
        if (isFirst) { //防止因为多次切换而阻塞多个线程
            game.executor.pause(); //阻塞线程直到接收到通知
        }
    }

    //技能效果
    private void skillEffect(Hero target) {
        //降低攻击力
        game.ui.printMsg(target.getObviousName() + "行动结束前");
        target.dropAtkWithPrint(DROP_ATK);
        target.event.onAfterAction.addRunBack(eventData -> {
            game.ui.printMsg(target.getObviousName() + "力量恢复");
            target.addAtkWithPrint(DROP_ATK);
            eventData.removeRunBackFromListener();
        });
        //清除仇恨
        game.ui.printMsg(target.getObviousName() + "放弃了对" + hostHero.getObviousName() + "的仇恨。");
        if (target.state.getAliveEnemy() == hostHero) {
            target.clearEnemy();
        }
        if (target.state.getAlivePrey() == hostHero) {
            target.clearPrey();
        }
        //恢复生命值
        game.ui.printMsg(target.getObviousName());
        target.addHpWithPrint(HEALING_AMOUNT);
        //自己也恢复一些生命值
        game.ui.printMsg(hostHero.getObviousName());
        hostHero.addHpWithPrint(HEALING_AMOUNT);
    }

    @Override
    protected void onUpdate() {

    }

    @Override
    protected void onInit() {

    }
}
